#!/usr/bin/perl

use warnings;
use strict;

use IO::Socket;
use Term::ReadKey;
use locale;


ReadMode('cbreak');
print "Press keys to see their ASCII values.  Use Ctrl-C to quit.\n";


my $numArgs = $#ARGV + 1;

($numArgs == 3) or die "please specify hostname, port and filename.\n";
my $remote_host = $ARGV[0];
my $remote_port = $ARGV[1];

my $filename = $ARGV[2];

open(SOURCE, "< $filename")
    or die "Couldn't open $filename for reading: $!\n";

ReadMode('cbreak');

my $socket = IO::Socket::INET->new(PeerAddr => $remote_host,
                                PeerPort => $remote_port,
                                Proto    => "tcp",
                                Type     => SOCK_STREAM)
    or die "Couldn't connect to $remote_host:$remote_port : $@\n";

while (<SOURCE>) {
 s/[^A-Za-z]/ /g;
  print $socket $_;

  print "send \"$_\".\n";
#  print ("wait for user's keyboard.\n");
  my $char;
  my $prechar;

  while (1) { 
    $char = ReadKey(0);
    if (defined $char) {
      $prechar = $char;
    }
    last unless defined $char;
    last;
  }
  if (ord($prechar) == ord('q'))
    {
      last;
    }
}

ReadMode('normal');






